<%@ Page language="vb" CodeFile="editDealer.aspx.vb" Inherits="editDealer_aspx_vb" %>

<%
    rowNumber = 0
    If (Request.QueryString("do")) = Nothing Then 
           subDo = Request.QueryString("subDo")
        dealerID = Request.QueryString("dealerID")
        dealerID = CLng(dealerID)
        dbOpen()
        RecSet = Conn.Execute("Select d.partnerID, d.dealer, d.pwd, d.city, d.areacode, d.telephone, d.email, c.countryID, c.country From (tblDealer d INNER JOIN tblCountry c ON c.countryID = d.countryID) Where d.dealerID = " & dealerID & "")
        If RecSet.EOF Then 
            Response.Redirect("searchDealer.aspx")
        Else
            dbPartnerID = RecSet.Fields("partnerID").Value
            dealer = RecSet.Fields("dealer").Value
            pwd = RecSet.Fields("pwd").Value
            city = RecSet.Fields("city").Value
            areacode = RecSet.Fields("areaCode").Value
            telephone = RecSet.Fields("telephone").Value
            email = RecSet.Fields("email").Value
            country = RecSet.Fields("country").Value
            countryID = RecSet.Fields("countryID").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        dbOpen()
        RecSet = Conn.Execute("Select c.countryCurrency, (Select Count(v.vehicleID) From tblVehicle v Where v.dealerID = d.dealerID) As vehicleCount, (Select Count(c.claimID) From (tblClaim c INNER JOIN tblVehicle v ON v.vehicleID = c.vehicleID) Where v.dealerID = d.dealerID) As claimCount, (Select Sum(c.paymentAmount) From (tblClaim c INNER JOIN tblVehicle v ON v.vehicleID = c.vehicleID) Where v.dealerID = d.dealerID) As claimSum From (tblDealer d INNER JOIN tblCountry c ON c.countryID = d.countryID) Where d.dealerID = " & dealerID & "")
        If Not RecSet.EOF Then 
            countryCurrency = RecSet.Fields("countryCurrency").Value
            vehicleCount = RecSet.Fields("vehicleCount").Value
            claimCount = RecSet.Fields("claimCount").Value
            claimSum = RecSet.Fields("claimSum").Value
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        If IsDBNull(claimSum) Then 
            claimSum = 0
        End IF
        If CDbl(vehicleCount) = 0 Then 
%>
<script type="text/javascript">
    function deleteCheck() {
        if (window.confirm('OBS! Are you sure you want to delete this dealer?')) {
            location.href = 'editDealer.aspx?dealerID=<%= dealerID %>&do=delete';
        }
        else {
            return false;
        }
    }
  </script>
<%
        End IF
        Over()
        strJS = ""
        strJS = strJS + "[""partnerID"", ""<1""],"
        strJS = strJS + "[""dealer"", ""==''""],"
        strJS = strJS + "[""city"", ""==''""]"
        checkFields()
        Response.Write("<table cellpadding='0' cellspacing='0' border='0' width='1000'>")
        Response.Write("<tr>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='3'>EDIT CAR DEALER<font color='#fcbc00'>" & subDo & "</font><hr class='hrHeadline'></td></tr>")
        Response.Write("<form action='editDealer.aspx?do=save&dealerID=" & dealerID & "' method='post' name='store_productsSearch' onsubmit=""javascript:return checkFields();"">")
        Response.Write("<tr><td class='formBold'>Country:</td><td align='right'><input type='text' name='country' id='country' value='" & country & "' size='40' maxlength='50' readonly='readonly'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td class='formBold'>Partner:</td>")
        Response.Write("<td align='right'>")
        Response.Write("<select name='partnerID' id='partnerID'>")
        Response.Write("<option value=''> - Select partner - ")
        dbOpen()
        RecSet = Conn.Execute("Select partnerID, partnerName From tblPartner Where countryID = " & countryID & " Order by partnerName")
        If Not RecSet.EOF Then 
            Do Until RecSet.EOF
                partnerID = RecSet.Fields("partnerID").Value
                partnerName = RecSet.Fields("partnerName").Value
                Response.Write("<option value='" & partnerID & "'")
                If dbPartnerID = CStr(partnerID) Then 
                    Response.Write(" selected='selected'")
                End IF
                Response.Write("> " & partnerName & " ")
                RecSet.MoveNext()
            Loop
        End IF
        RecSet.Close()
        RecSet = Nothing
        dbClose()
        Response.Write("<option value=''> -------------------------------------- ")
        Response.Write("</select>")
        Response.Write("</td>")
        Response.Write("<td><div id='error_partnerID' class='edit_error'>&laquo; OBS!</div></td>")
        Response.Write("</tr>")
        Response.Write("<tr><td class='formBold'>Car dealer:</td><td align='right'><input type='text' name='dealer' id='dealer' value='" & dealer & "' size='40' maxlength='50'></td><td><div id='error_dealer' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Password:</td><td align='right'><input type='text' name='pwd' id='pwd' value='" & pwd & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>City:</td><td align='right'><input type='text' name='city' id='city' value='" & city & "' size='40' maxlength='50'></td><td><div id='error_city' class='edit_error'>&laquo; OBS!</div></td></tr>")
        Response.Write("<tr><td class='formBold'>Areacode:</td><td align='right'><input type='text' name='areacode' value='" & areacode & "' id='areacode' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Telephone:</td><td align='right'><input type='text' name='telephone' id='telephone' value='" & telephone & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr><td class='formBold'>Email:</td><td align='right'><input type='text' name='email' id='email' value='" & email & "' size='40' maxlength='50'></td></tr>")
        Response.Write("<tr>")
        Response.Write("<td align='right' colspan='2'>")
        If CDbl(vehicleCount) = 0 Then 
            Response.Write("<input type='button' value='Delete dealer' onclick=""{return deleteCheck();} return false;"">&nbsp; ")
        End IF
        Response.Write("<input type='submit' value='Save changes'>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</form>")
        Response.Write("<tr><td colspan='3'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("<td width='100'></td>")
        Response.Write("<td valign='top'>")
        Response.Write("<table cellpadding='2' cellspacing='2' width='450'>")
        Response.Write("<tr><td class='headline' colspan='2'>DEALER STATISTICS<hr class='hrHeadline'></td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Number of lease contracts:</td><td align='right'>" & vehicleCount & "</td></tr>")
        Response.Write("<tr><td>Number of claims reported:</td><td align='right'>" & claimCount & "</td></tr>")
        Response.Write("<tr bgcolor='#eeeeee'><td>Total claim cost (" & countryCurrency & "):</td><td align='right'>" & FormatNumber(CDbl(claimSum), 2) & "</td></tr>")
        Response.Write("<tr><td colspan='2'><hr class='hrHeadline'></td></tr>")
        Response.Write("</table>")
        Response.Write("</td>")
        Response.Write("</tr>")
        Response.Write("</table>")
        Under()
    ElseIf Request.QueryString("do") = "save" Then 
        dealerID = Request.QueryString("dealerID")
        partnerID = Request.Form("partnerID")
        dealer = Request.Form("dealer")
        pwd = Request.Form("pwd")
        city = Request.Form("city")
        areacode = Request.Form("areaCode")
        telephone = Request.Form("telephone")
        email = Request.Form("email")
        userID = fragusLeasingUserID
        dealerID = CLng(dealerID)
        partnerID = CLng(partnerID)
        If Len(dealer) > 0 Then 
            dealer = Trim(dealer)
            dealer = Replace(CStr(dealer), "'", "")
            dealer = Replace(CStr(dealer), """", "")
            dealer = Replace(CStr(dealer), "<", "")
            dealer = Replace(CStr(dealer), ">", "")
            dealer = Replace(CStr(dealer), "=", "")
        End IF
        If Len(city) > 0 Then 
            city = Trim(city)
            city = Replace(CStr(city), "'", "")
            city = Replace(CStr(city), """", "")
            city = Replace(CStr(city), "<", "")
            city = Replace(CStr(city), ">", "")
            city = Replace(CStr(city), "=", "")
        End IF
        If Len(areacode) > 0 Then 
            areacode = Trim(areacode)
            areacode = Replace(CStr(areacode), "'", "")
            areacode = Replace(CStr(areacode), """", "")
            areacode = Replace(CStr(areacode), "<", "")
            areacode = Replace(CStr(areacode), ">", "")
            areacode = Replace(CStr(areacode), "=", "")
        End IF
        If Len(telephone) > 0 Then 
            telephone = Trim(telephone)
            telephone = Replace(CStr(telephone), "'", "")
            telephone = Replace(CStr(telephone), """", "")
            telephone = Replace(CStr(telephone), "<", "")
            telephone = Replace(CStr(telephone), ">", "")
            telephone = Replace(CStr(telephone), "=", "")
        End IF
        If Len(email) > 0 Then 
            email = Trim(email)
            email = Replace(CStr(email), "'", "")
            email = Replace(CStr(email), """", "")
            email = Replace(CStr(email), "<", "")
            email = Replace(CStr(email), ">", "")
            email = Replace(CStr(email), "=", "")
        End IF
        dbOpen()
        Conn.Execute("Update tblDealer Set userID = " & userID & ", partnerID = " & partnerID & ", dealer = '" & dealer & "', pwd = '" & pwd & "', city = '" & city & "', areacode = '" & areacode & "', telephone = '" & telephone & "', email = '" & email & "' Where dealerID = " & dealerID & "")
        dbClose()
        If Len(pwd) = 0 Then 
            dbOpen()
            Conn.Execute("Update tblDealer Set pwd = NULL Where dealerID = " & dealerID & "")
            dbClose()
        End IF
        Response.Redirect("editDealer.aspx?subDo=%20-%20SAVED&dealerID=" & dealerID & "")
    ElseIf Request.QueryString("do") = "delete" Then 
        dealerID = Request.QueryString("dealerID")
        dealerID = CLng(dealerID)
        dbOpen()
        Conn.Execute("Delete From tblDealer Where dealerID = " & dealerID & "")
        dbClose()
        Response.Redirect("addDealer.aspx?subDo=%20-%20DELETED")
    End IF

%>
